#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <unordered_map>
#include "util.hpp"
namespace ns_index
{
    struct DocInfo
    {
        std::string title;   // 文档的标题
        std::string content; // 文档对应的去标签之后的内容
        std::string url;     // 官网文档的url
        u_int64_t doc_id;
    };
    struct InvertedElem
    {
        u_int64_t doc_id;
        std::string words;
        int weight;
    };
    // 倒排拉链
    typedef std::vector<InvertedElem> InvertedList;
    class Index
    {
    private:
        // 正派索引的数据结构用数组，数组的下标天然石文档的ID
        std::vector<DocInfo> forward_index;
        // 倒排索引一定是一个关键字和一组（个）InvertedElem对应[关键字和倒排拉链的映射关系]
        std::unordered_map<std::string, std::vector<InvertedElem>> inverted_index;

    public:
        Index() {}
        ~Index() {};

    public:
        // 根据doc_id找到文档内容
        DocInfo *GetForwardIndex(const u_int64_t doc_id)
        {
            if (doc_id >= forward_index.size())
            {
                std::cerr << "doc_id out range.error" << std::endl;
                return nullptr;
            }
            return &forward_index[doc_id];
        }
        // 根据关键字string，获取倒排拉链
        InvertedList *GetInvertedListt(const std::string &word)
        {
            auto iter = inverted_index.find(word);
            if (iter == inverted_index.end())
            {
                std::cerr << word << "have no InvertedList" << std::endl;
                return nullptr;
            }
            return &(iter->second);
        }
        // 根据文档的去标签格式化之后的文档，构建正排和倒排索引
        bool BuildIndex(const std::string &input)
        {
            std::ifstream in(input, std::ios::in | std::ios::binary);
            if (!in.is_open())
            {
                std::cerr << "sorry." << input << "open error" << std::endl;
                return false;
            }
            std::string line;
            while (std::getline(in, line))
            {
                DocInfo *doc = BuildForwardIndex(line);
                if (nullptr == doc)
                {
                    std::cerr << "build：" << line << "error" << std::endl;
                    continue;
                }
                BuildInvertIndex(*doc);
            }
            return true;
        }

    private:
        DocInfo *BuildForwardIndex(const std::string &line)
        {
            // 1.解析里，字符串切分
            std::vector<std::string> results;
            std::string sep = "\3"; // 行内分隔符
            ns_util::StringUtil::CutString(line, &results, sep);
            // ns_util::StringUtil::CutString(line,&results,sep);
            if (results.size() != 3)
            {
                return nullptr;
            }
            // 2.字符串进行填充到DocIinfo
            DocInfo doc;
            doc.title = results[0];
            doc.content = results[1];
            doc.url = results[2];
            doc.doc_id = forward_index.size(); // 先进行保存id，再插入，对应id久视当前doc在vec中的下标。

            // 3.插入到正排索引的vector
            forward_index.push_back(doc);
            return &forward_index.back();
        }
        bool BuildInvertIndex(const DocInfo &doc)
        {
            //DocInfo
            return true;
        }
    };
}
